package com.todod.base.module.base.service.protocolstandard;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.todod.base.module.base.controller.admin.protocolstandard.vo.*;
import com.todod.base.module.base.dal.dataobject.protocolstandard.ProtocolStandardDO;
import com.todod.base.framework.common.pojo.PageResult;
import com.todod.base.framework.common.pojo.PageParam;
import com.todod.base.framework.common.util.object.BeanUtils;

import com.todod.base.module.base.dal.mysql.protocolstandard.ProtocolStandardMapper;

import static com.todod.base.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.todod.base.module.base.enums.ErrorCodeConstants.*;

/**
 * 协议标准 Service 实现类
 *
 * @author 平台
 */
@Slf4j
@Service
@Validated
public class ProtocolStandardServiceImpl implements ProtocolStandardService {

    @Resource
    private ProtocolStandardMapper protocolStandardMapper;

    @Override
    public Long createProtocolStandard(ProtocolStandardSaveReqVO createReqVO) {
        // 插入
        ProtocolStandardDO protocolStandard = BeanUtils.toBean(createReqVO, ProtocolStandardDO.class);
        protocolStandardMapper.insert(protocolStandard);
        // 返回
        return protocolStandard.getId();
    }

    @Override
    public void updateProtocolStandard(ProtocolStandardSaveReqVO updateReqVO) {
        // 校验存在
        validateProtocolStandardExists(updateReqVO.getId());
        // 更新
        ProtocolStandardDO updateObj = BeanUtils.toBean(updateReqVO, ProtocolStandardDO.class);
        protocolStandardMapper.updateById(updateObj);
    }


    @Override
    public void enable(Long id) {
        log.info("调用启用协议标准业务逻辑层");
        // 校验存在
        validateProtocolStandardExists(id);

        // 启用
        protocolStandardMapper.enable(id);
    }

    @Override
    public void disable(Long id) {
        log.info("禁用启用协议标准业务逻辑层");
        // 校验存在
        validateProtocolStandardExists(id);

        // 禁用
        protocolStandardMapper.disable(id);

    }

    @Override
    public void deleteProtocolStandard(Long id) {
        // 校验存在
        validateProtocolStandardExists(id);
        // 删除
        protocolStandardMapper.deleteById(id);
    }

    private void validateProtocolStandardExists(Long id) {
        if (protocolStandardMapper.selectById(id) == null) {
            throw exception(PROTOCOL_STANDARD_NOT_EXISTS);
        }
    }

    @Override
    public ProtocolStandardDO getProtocolStandard(Long id) {
        return protocolStandardMapper.selectById(id);
    }

    @Override
    public PageResult<ProtocolStandardDO> getProtocolStandardPage(ProtocolStandardPageReqVO pageReqVO) {
        return protocolStandardMapper.selectPage(pageReqVO);
    }

}